<template>
  <div class="search">
    <div class="search_block">
      <div class="search_box">
        <img class="search_icon" src="../../assets/icons/icon_search.png" />
        <input
          class="search_text"
          type="text"
          placeholder="搜索"
          @focus="focusInput"
          v-model="searchText"
          @input="changeInput"
        />
        <img
          class="cancel_btn"
          v-show="searchFocus"
          src="../../assets/icons/icon_delete_up.png"
          @click="cancelFocus"
        />
      </div>

      <el-popover
        popper-class="menu_popover"
        placement="bottom-end"
        width="97"
        trigger="click"
      >
        <ul class="menu_box">
          <li class="menu_line" @click="createGroup">
            <img class="menu_pic" src="../../assets/icons/icon_addGroup_up.png" />
            <span class="menu_text">创建群聊</span>
          </li>
          <li class="menu_line" @click="addFriendDialog">
            <img class="menu_pic" src="../../assets/icons/icon_addFriend_up.png" />
            <span class="menu_text">添加好友</span>
          </li>
        </ul>
        <img
          class="add_btn"
          slot="reference"
          src="../../assets/icons/icon_add.png"
        />
      </el-popover>
    </div>

    <el-dialog
      :visible.sync="addFriendState"
      width="560px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-dialog
        :visible.sync="addFriendConfirm"
        width="365px"
        :show-close="false"
        :close-on-click-modal="false"
        append-to-body
      >
        <div class="dialog_top">
          <div class="dialog_title">添加好友</div>
          <img
            class="cancel_dialog_btn"
            src="../../assets/icons/icon_close_up.png"
            @click="cancelFriendClick"
          />
        </div>
        <div class="user_container">
          <img class="user_sculpture" :src="addFriendItem.headImg" />
          <div class="user_block">
            <div class="user_name">{{ addFriendItem.userName }}</div>
            <div class="user_area">广东省 广州市</div>
          </div>
        </div>
        <div class="user_bottom">
          <div class="comment_box">
            <input
              class="comment_input"
              type="text"
              maxlength="30"
              v-model="addFriendRemark"
            />
            <span class="comment_num">{{ addFriendRemark.length }}/30</span>
          </div>
          <div class="primarybtn" @click="sendAddApply">发送</div>
        </div>
      </el-dialog>

      <div class="dialog_top">
        <div class="dialog_title">添加好友</div>
        <img
          class="cancel_dialog_btn"
          src="../../assets/icons/icon_close_up.png"
          @click="cancelDialog"
        />
      </div>
      <div class="user_search">
        <div class="search_content">
          <img class="search_icon" src="../../assets/icons/icon_search.png" />
          <input
            class="search_input"
            type="text"
            placeholder="输入昵称"
            maxlength="30"
            v-model="userText"
            @keyup="enterPress($event)"
          />
        </div>
        <div class="primarybtn" @click="findUser">查找</div>
      </div>

      <ul class="user_list" ref="user_list" @scroll="scrollWatch">
        <div class="list_box" ref="list_box">
          <li class="user_box" v-for="(item, index) in userList" :key="index">
            <img class="user_avatar" :src="item.headImg" />
            <div class="user_text">
              <div class="user_nick">{{ item.userName }}</div>
              <div class="user_place">广东省 广州市</div>
            </div>
            <div class="add_user" @click="addFriendClick(item)">加好友</div>
          </li>
        </div>
      </ul>
    </el-dialog>

    <chooseMembers
      :chooseUserData="chooseUserData"
      v-show="chooseUserData.chooseUserState"
      @changeChooseState="changeChooseState"
    />
  </div>
</template>

<script>
import chooseMembers from "../choose_members";
import { Popover, Dialog, Message } from "element-ui";
import { searchUser, addUser, friendsList } from "../../api/api";
import bus from "../../bus";
export default {
  name: "search",
  data() {
    return {
      searchText: "",
      userText: "",
      searchFocus: false,
      addFriendState: false,
      addFriendConfirm: false,
      chooseUserData: {
        type: "",
        chooseUserState: false,
      },
      userList: [],
      addFriendItem: {},
      addFriendRemark: "",
      page: 1,
    };
  },
  components: {
    chooseMembers,
    "el-popover": Popover,
    "el-dialog": Dialog,
  },
  computed: {
    newMessage() {
      return this.$store.state.message
    }
  },
  watch:{
    //收到的消息
    newMessage(){
      if(this.newMessage.code==10013||this.newMessage.code==10014){
        Message({
          message: '不能重复申请添加好友',
          type: 'error'
        })
      }else if(this.newMessage.code==10015){
        Message({
          message: '已是好友，不能再次添加',
          type: 'error'
        })
      }else if(this.newMessage.code===0&&this.newMessage.command=='COMMAND_JOIN_FRIEND_ACTION_RESP'){
        Message({
          message: "发送好友申请成功",
          type: "success",
        })
      }
    }
  },
  methods: {
    //按回车键
    enterPress(event){
      var e = event || window.event, ec = e.keyCode || e.which;
      //按回车发送时
      if(e.which==13){
        this.findUser()
      }
    },
    //搜索好友
    findUser() {
      this.userList = []
      this.getUserList()
    },
    getUserList(){
      let params = {
        searchContent: this.userText,
        pageNo: this.page,
        pageSize: 10,
      }
      searchUser(params)
      .then((res) => {
        this.userList = this.userList.concat(res.data.data.list);
      })
    },
    //发送添加好友申请
    sendAddApply() {
      var sendData = {
        cmd:11,
        ext:{
            token:this.$store.state.token,
            sendUserId:this.$store.state.user.id,
            sendUserImg:this.$store.state.user.headImg,
            sendUserName:this.$store.state.user.userName,
            toId:this.addFriendItem.id,
            toUserImg:this.addFriendItem.headImg,
            toUserName:this.addFriendItem.userName,
            reasons:this.addFriendRemark
        },
        type:41
      }
      console.log(sendData)
      ws.send(JSON.stringify(sendData))
      this.cancelFriendClick()
    },
    //搜索框聚焦
    focusInput() {
      this.searchFocus = true;
      this.$emit("changeSearchState", true);
    },
    //清空搜索框
    cancelFocus() {
      this.searchText = "";
      this.searchFocus = false;
      this.$emit("changeSearchState", false);
    },
    //搜索框输入字符
    changeInput() {
      let params = {
        userId: this.$store.state.user.id,
        friendRemarks: this.searchText,
      }
      //加载好友列表
      friendsList(params)
      .then((res) => {
        var list = res.data.data.list;
        bus.$emit("searchFriendList", list);
      })
    },
    //添加好友
    addFriendDialog() {
      this.addFriendState = true;
    },
    //点击加好友按钮，弹出弹窗
    addFriendClick(item) {
      this.addFriendItem = item;
      this.addFriendConfirm = true;
    },
    //关闭加好友弹窗
    cancelFriendClick() {
      this.addFriendConfirm = false;
    },
    cancelDialog() {
      this.addFriendState = false;
      this.userText = "";
    },
    //点击创建群聊
    createGroup() {
      this.chooseUserData.chooseUserState = true;
      this.chooseUserData.type = "createGroup";
    },
    changeChooseState(value) {
      this.chooseUserData = value;
    },
    //滚动事件,控制分页
    scrollWatch() {
      var userListHeight = this.$refs.user_list.offsetHeight;
      var listBoxHeight = this.$refs.list_box.offsetHeight;
      var userListOffsetTop = this.$refs.user_list.scrollTop;
      // console.log(userListHeight)
      // console.log(listBoxHeight)
      // console.log(userListOffsetTop)
      if (userListHeight - (listBoxHeight - userListOffsetTop) >= 30) {
        this.page++;
        this.getUserList();
      }
    },
  },
};
</script>

<style scoped>
.search_block {
  padding: 13px 12px 13px 11px;
  position: relative;
  display: flex;
  align-items: center;
}
.search_box {
  width: 217px;
  height: 30px;
  border-radius: 17px;
  margin-right: 12px;
  background: #f6f6f6;
  padding-left: 12px;
  display: flex;
  align-items: center;
  box-sizing: border-box;
}
.search_icon {
  width: 20px;
}
.cancel_btn {
  width: 20px;
  margin-right: 5px;
  margin-left: 5px;
  cursor: pointer;
}
.search_text {
  background: #f6f6f6;
  font-size: 12px;
  margin-left: 3px;
  flex: 1;
  border: none;
}
.add_btn {
  width: 25px;
  height: 25px;
  cursor: pointer;
}
.menu_line {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 40px;
  cursor: pointer;
}
.menu_line:hover {
  background: #edeff3;
}
.menu_pic {
  width: 20px;
}
.menu_text {
  margin-left: 2px;
  font-size: 14px;
  color: #333;
}

.dialog_top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 42px;
  padding: 0 25px 0 30px;
}
.cancel_dialog_btn {
  width: 24px;
  height: 24px;
  cursor: pointer;
}
.user_search {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 25px 0 30px;
}
.user_search .search_icon {
  padding: 0px 8px;
}
.search_input {
  min-width: 280px;
  color: #428dfc;
  line-height: 30px;
  border: none;
}
.user_list {
  padding: 10px 30px 20px;
  overflow: auto;
  height: 450px;
}
.user_box {
  padding: 10px;
  display: flex;
  align-items: center;
}
.user_avatar {
  width: 40px;
  height: 40px;
}
.user_text {
  flex: 1;
  margin: 0 10px;
}
.user_nick {
  font-size: 14px;
  color: #333;
}
.user_area {
  color: #b9b9b9;
  font-size: 12px;
}
.add_user {
  text-align: center;
  cursor: pointer;
  width: 75px;
  height: 30px;
  line-height: 30px;
  border-radius: 4px;
  font-size: 12px;
  background: #fff;
  border: 1px solid #428dfc;
  color: #428dfc;
}
.user_container {
  display: flex;
  align-items: center;
  width: 304px;
  padding: 10px;
  border: 1px solid #f1f1f1;
  border-radius: 4px;
  margin: 0 auto;
}
.user_sculpture {
  width: 60px;
  height: 60px;
  margin-right: 10px;
}
.user_name {
  font-size: 14px;
  color: #333;
  margin-bottom: 3px;
}
.user_place {
  font-size: 12px;
  color: #999;
}
.user_bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 30px;
}
.comment_box {
  display: flex;
  align-items: center;
  background: #f8f8f8;
  border-radius: 4px;
  margin-right: 15px;
  line-height: 30px;
}
.comment_input {
  border: none;
  background: transparent;
}
.comment_num {
  color: #c9c9c9;
  font-size: 12px;
  float: right;
  padding-right: 7px;
  white-space: nowrap;
}
</style>